package main

import (
	"context"
	"fmt"
	"math/rand"
	"net"
	"test1/protocol"
	"time"

	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

type server struct {
	protocol.UnimplementedShufflerServer
}

func (s *server) ShuffleCards(ctx context.Context, in *protocol.SCardsRequest) (*protocol.SCardsReply, error) {
	fmt.Printf("收到洗牌请求: %v\n", in.CardsArr)
	return &protocol.SCardsReply{CardsArr: Shuffle(in.CardsArr)}, nil
}

func main() {
	lis, err := net.Listen("tcp", ":8080")
	if err != nil {
		fmt.Printf("监听端口失败: %s", err)
		return
	}
	rand.Seed(time.Now().UnixNano())

	// 创建gRPC服务器
	s := grpc.NewServer()
	// 注册服务
	protocol.RegisterShufflerServer(s, &server{})

	reflection.Register(s)

	err = s.Serve(lis)
	if err != nil {
		fmt.Printf("开启服务失败: %s", err)
		return
	}
}
